<template>
  <el-dialog :title="titleValue" :visible="showDialog" @close="close">
    <!-- <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
    </el-form> -->
    <!-- 放置表单内容 -->
    <el-form ref="addForm" label-width="120px" :rules="formRules" :model="addForm">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="addForm.name" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="addForm.code" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="addForm.managerId" style="width: 80%;" size="mini" placeholder="请选择负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          /></el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="addForm.introduce"
          style="width: 80%;"
          size="mini"
          type="textarea"
          :rows="4"
          placeholder="1-100个字符"
        />
      </el-form-item>
    </el-form>

    <!-- slot="footer" v-slot="{ data }" v-slot:footer #footer === slot='footer' -->
    <!-- slot="footer" v-slot:footer #footer(必须用在template标签上) -->
    <template #footer>
      <el-row style="width:100%;" type="flex" justify="end">
        <el-button size="mini" type="text" style="color: #727479;margin-right:20px" @click="close">取消</el-button>
        <el-button type="primary" size="mini" @click="btnOk">确认</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addDeptAPI, deleteDeptDetailAPI, getDepartmentMsgAPI, getDeptDetailAPI, getManagerList, updateDeptDetailAPI } from '@/api/department'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      addForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      formRules: {
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '编码为2-10个字符', trigger: 'blur' },
          { validatoe: async(rule, value, callback) => {
            const list = await getDepartmentMsgAPI()
            list.filter(el => { el.id !== this.addForm.id }).some(el => el.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
          }, trigger: 'blur' }
        ],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ],
        managerId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            const list = await getDepartmentMsgAPI()
            list.filter(el => { el.id !== this.addForm.id }).some(el => el.item === value) ? callback(new Error(`当前部门已有${value}名称`)) : callback()
          }, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    titleValue() {
      return this.addForm.id ? '编辑部门' : '新增子部门'
    }
  },
  created() {
    this.getMangerMsg()
  },
  methods: {
    close() {
      // 清空addForm的值
      this.addForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      // el组件的重置表单
      this.$refs.addForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async btnOk() {
      await this.$refs.addForm.validate()
      if (this.addForm.id) {
        await updateDeptDetailAPI(this.addForm)
        this.$message.success('编辑部门成功')
      } else {
        await addDeptAPI({ ...this.addForm, pid: this.pid })
        this.$message.success('新增部门成功')
      }
      // 关闭弹窗
      this.close()

      // 要通知父组件进行界面更新
      this.$emit('updateDept')
    },
    async getMangerMsg() {
      this.managerList = await getManagerList()
    },
    // 1.编辑表单的方法
    async getDeptDetail(id) {
      // 2.有自身id
      this.addForm = await getDeptDetailAPI(id)
    },
    async deleteDeptDetail(id) {
      await deleteDeptDetailAPI(id)
      this.$message.success('删除成功')
      this.$emit('updateDept')
    }
  }
}
</script>

<style>

</style>
